/**
 *  公共格式类,格式化数字
 */
//每隔三位小数加逗号
function toThousands(num) {
    var num = (num || 0).toString();
    var result = ''; //整数
    var decimal = ""; //小数位数+单位
    var symbol = ""; //符号位
    if(num.indexOf("-") != -1){//包含符号位
        symbol = "-";
        num = num.substring(1,num.length); //因为符号位 只占一位
    }
    if(num.indexOf(".") != -1){ //包含小数位
        decimal = "."+num.substring(num.indexOf(".")+1,num.length); //小数后面的数据
        num = num.substring(0,num.indexOf(".")); //整数
    }
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
    }
    if (num) {
        result = num + result;
    }
    return symbol+result+decimal;
}

/**
 * 获去本年度
 * @returns {number}
 */
Date.prototype.getWeek=function(){
    var firstDay = new Date(this.getFullYear(),0,1);
    var weekFirstDay = firstDay.getDay();
    if (weekFirstDay!=0)
        firstDay = new Date(this.getFullYear(),0,8-weekFirstDay);
    var week = Math.ceil((this.getTime()-firstDay.getTime())/(1000*60*60*24*7))+1;
    return week;
}

Date.prototype.format =function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "w+" : this.getWeek(),  //week
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format))
        format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length==1? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}
/**
 * 时间字符串格式化
 * @param patten
 * @returns {string}
 */
String.prototype.format=function(patten){
    var p1 = /^(\d{4})(\d{2})$/;
    var p2 = /^(\d{4})(\d{2})(\d{2})$/;
    if (p1.test(this)) {
        return this.replace(p1,RegExp.$1+patten+RegExp.$2);
    } else if (p2.test(this)) {
        return this.replace(p2,RegExp.$1+patten+RegExp.$2+patten+RegExp.$3);
    }
    return this.toString();
}

/**
 * 获取系统当前时间
 * @returns {string}
 */
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    //alert("currentdate:"+currentdate)
    return currentdate;
}


/**
 * 日期格式化 2019年9月24日 星期二
 * @returns {string}
 */
function getNowFormatDate2() {
    var date = new Date();
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.date = date.getDate();
    this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
    this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    var currentTime =  this.year + "年" + this.month + "月" + this.date + "日  " + this.day;
    return currentTime;
}
